package com.aya.transaction;

import com.aya.transaction.service.BlogRequiredTransactionService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

/**
 * 主事物S下面有事物A和事物B
 * 最终被合并成一个事物S.
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringTransaction.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class RequiredTransaction {

    @Resource
    BlogRequiredTransactionService blogRequiredTransactionService;

    /**
     * 事物A和事物B都成功了
     * 进行整体的提交
     */
    @Test
    public void testSuccess() {
        blogRequiredTransactionService.updateByIdSuccess("1", "transaction success","2","success");
    }

    /**
     * 场景:
     * 事物A成功
     * 事物B失败
     * 结果:
     * 回滚S事物
     */
    @Test
    public void testSupportFailure() {
        blogRequiredTransactionService.updateByIdInnerFailure("1", "transaction success","2","not support failure");
    }

    /**
     * 场景:
     * 事物A成功
     * 事物B成功
     * 事物S抛出异常
     * 结果:
     * 回滚S事物
     */
    @Test
    public void testFailure() {
        blogRequiredTransactionService.updateByIdOuterFailure("1", "transaction failure","2","failure");
    }

    /**
     * 场景:
     * 事物A成功
     * 事物S捕获 事物B失败
     * 结果:
     * 回滚S事物，并且以后所有的执行都不会提交
     */
    @Test
    public void testCatchFailure() {
        blogRequiredTransactionService.updateByIdCatchFailure("1", "transaction failure","2","failure");
    }

    /**
     * 场景:
     * 事物中
     * A成功，B失败。
     *
     */
    @Test
    public void testDuplicateFailure() {
        blogRequiredTransactionService.updateByDuplicate("1", "success","2","duplicate");
    }

}
